package com.wika.basics.utils

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Outline
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.view.View
import android.view.ViewOutlineProvider
import android.view.WindowManager
import androidx.core.content.ContextCompat

/**
 * @Description: View相关处理工具类
 * @Date: 2023/12/18 11:26
 * @Author: WangWeiShuo
 */
object ViewUtils {

    /**
     * 设置View圆角
     */
    fun setClipViewCornerRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, 0, view?.width ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置View顶部圆角
     */
    fun setClipViewCornerTopRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, 0, view?.width ?: 0, view?.height?.plus(radius) ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置View底部圆角
     */
    fun setClipViewCornerBottomRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, -radius, view?.width ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }
    /**
     * 设置View左边圆角
     */
    fun setClipViewCornerLeftRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, 0, view?.width?.plus(radius) ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置View右边圆角
     */
    fun setClipViewCornerRightRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(-radius, 0, view?.width ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置透明沉浸式状态栏
     * @param activity 页面
     * @param isDarkText 是否状态栏文字显示深色
     */
    fun transparentBar(activity: Activity, isDarkText: Boolean = false) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity?.window
            window?.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window?.statusBarColor = Color.TRANSPARENT
            var systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            if (isDarkText && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window?.decorView?.systemUiVisibility = systemUiVisibility
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window = activity?.window
            window?.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            )
        }
    }

    /**
     * 动态设置控件的selector
     * @param context Context
     * @param view View
     * @param norMalColor Int
     * @param pressedColor Int
     */
    fun setViewSelectorByColorId(context: Context,view: View,norMalColorId: Int,pressedColorId: Int){
        val selector = StateListDrawable()
        val normalDrawable = ColorDrawable()

        normalDrawable.color = ContextCompat.getColor(context, norMalColorId)
//        normalDrawable.setShape(GradientDrawable.RECTANGLE);
//        normalDrawable.setCornerRadius(DensityUtil.dip2px(UIUtils.getContext(), 5));

//        val pressDrawable = resources.getDrawable(android.R.drawable.pressDrawableId)
//        val selectDrawable = resources.getDrawable(android.R.drawable.selectDrawableId)
//        val defaultDrawable = resources.getDrawable(android.R.drawable.defaultDrawableId)

        val pressedDrawable = ColorDrawable()
        pressedDrawable.color = ContextCompat.getColor(context, pressedColorId)

        selector.addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)
        selector.addState(intArrayOf(-android.R.attr.state_pressed), normalDrawable)
        // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效

        selector.addState(intArrayOf(), normalDrawable)
        view.background = selector
    }

    /**
     * 动态设置控件的selector
     * @param context Context
     * @param view View
     * @param norMalColor Int
     * @param pressedColor Int
     */
    fun setViewSelector(context: Context,view: View,norMalColor: Int,pressedColor: Int){
        val selector = StateListDrawable()
        val normalDrawable = ColorDrawable()

        normalDrawable.color = norMalColor
//        normalDrawable.setShape(GradientDrawable.RECTANGLE);
//        normalDrawable.setCornerRadius(DensityUtil.dip2px(UIUtils.getContext(), 5));

//        val pressDrawable = resources.getDrawable(android.R.drawable.pressDrawableId)
//        val selectDrawable = resources.getDrawable(android.R.drawable.selectDrawableId)
//        val defaultDrawable = resources.getDrawable(android.R.drawable.defaultDrawableId)

        val pressedDrawable = ColorDrawable()
        pressedDrawable.color = pressedColor

        selector.addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)
        selector.addState(intArrayOf(-android.R.attr.state_pressed), normalDrawable)
        // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效

        selector.addState(intArrayOf(), normalDrawable)
        view.background = selector
    }
    /**
     * dp转换成px
     */
    fun dp2px(context: Context, dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    /**
     * px转换成dp
     */
    fun px2dp(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * sp转换成px
     */
    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

    /**
     * px转换成sp
     */
    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }
}